<?php

/**
 * Step models
 *
 * Step models in bundles
 *
 * PHP Version: 1.2.3
 *
 * @author Johari <joharijumali@gmail.com>
 */

namespace Admin\Models\Modul;

use \Laravel\Database\Eloquent\Model as Eloquent;
use Datagrid;
use Admin\Models\Modul\Flow as Flow;
use Laravel\Config;

/**
 * Data Step Model Class
 *
 * Create model class for lookup data
 *
 * @author Johari <joharijumali@gmail.com>
 */

class Step extends Eloquent
{

    public static $timestamps = true;
    public static $table = 'sys_flow_step';
    public static $key = 'sfs_id';

	/**
     * Function listSteps that pass parameter
     *
     * @param integer $flowid the integer to quote
	 *
	 * @return parameter.
     */
    public static function listSteps()
	{
        $flowSteps = Step::paginate(Config::get('system.pagination'));

        $datagrid = new Datagrid;
        $datagrid->setFields(array('sfs_step' => 'Step'));
        $datagrid->setAction('edit', 'editStep', true, array(static::$key));
        $datagrid->setAction('delete', 'deleteStep', true, array(static::$key,'sfs_step'));
        $datagrid->setTable('steps', 'table table-bordered table-hover table-striped table-condensed');
        $datagrid->build($flowSteps, static::$key);

        return $datagrid->render();
    }
	
	/**
     * Function role
	 *
	 * @return role.
     */
    public function role()
	{
    	return $this->has_many('Admin\Models\User\Role', 'sur_id');
    }
	
	/**
     * Function arraySteps
	 *
	 * @return arraySteps.
     */
	public static function arraySteps()
	{
		$steplist = Step::all();

		$arraySteps = array();
		$arraySteps[0] = 'Choose Steps';
		foreach ($steplist as $value) {

			$arraySteps[$value->sfs_id] = $value->sfs_step;
		}

		return $arraySteps;
	}
	
	/**
     * Function steploop that pass parameter
     *
     * @param integer $flowid the integer to quote
	 *
	 * @return parameter.
     */
    public static function steploop($flowid)
	{
        $trees = array();
        $first = Step::where('parentid', '=', 0)
			->where('flowid', '=', $flowid)->get();

        if ($first) {
            foreach ($first as $value) {
                if (!empty($value)) {
                    $trees[$value->stepid]['desc'] = $value->step;
                    $trees[$value->stepid]['child'] = self::looper($value->stepid, $flowid);
                }
            }
        }

        return $trees;
    }
	
	/**
     * Function looper that pass parameter
     *
     * @param integer $id     the integer to quote
     * @param integer $flowid the integer to quote
	 *
	 * @return parameter.
     */
    protected static function looper($id, $flowid)
	{
        $trees = array();
        $child = Step::where('parentid', '=', $id)
			->where('flowid', '=', $flowid)->get();

        foreach ($child as $cvalue) {
            if (!empty($cvalue)) {
                $trees[$cvalue->stepid]['desc'] = $cvalue->step;
                $trees[$cvalue->stepid]['child'] = self::looper($cvalue->stepid, $flowid);
            }
        }

        return $trees;
    }

}

?>